﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using GalaSoft.MvvmLight;
using SD.Libs.Navigation.Contracts;
using GalaSoft.MvvmLight.Command;
using TestRunner.Common;
using TestRunner.Base;

namespace TestRunner.ViewModel
{
    public class Page2VM : MyVMBase
    {
        #region fields

        private INavigationService _navigationService;

        #endregion fields

        #region properties

        private RelayCommand _goToPage1Command;

        public RelayCommand GoToPage1Command
        {
            get
            {
                return _goToPage1Command;
            }

        }


        bool _canNavigateAway;

        public bool CanNavigateAway
        {
            get { return _canNavigateAway; }
            set { _canNavigateAway = value;
            RaisePropertyChanged("CanNavigateAway");
            }
        }

        #endregion properties

        #region constructor
                        
        public Page2VM(INavigationService navigationService)
        {
            if (IsInDesignMode)
            {
            }
            else
            {
                _navigationService = navigationService;
                _navigationService.Navigating += new System.Windows.Navigation.NavigatingCancelEventHandler(_navigationService_Navigating);
                this.Navigated += new NavigatedToPageEventHandler(Page2VM_Navigated);

                initializeCommands();

                CanNavigateAway = false;
                
            }
        }

        void Page2VM_Navigated(object sender, NavigationContextEventArgs e)
        {
            
        }

        #endregion constructor

        #region methods

        void initializeCommands()
        {
            _goToPage1Command = new RelayCommand(
                                                    () =>
                                                    {
                                                        _navigationService.NavigateTo(NavigationHelper.Home);
                                                    }
                                                    ,
                                                    null
                                                );


        }

        #endregion methods

        #region events

        void _navigationService_Navigating(object sender, System.Windows.Navigation.NavigatingCancelEventArgs e)
        { 
            // NavigationService Current Source != pagina actual                                    
           
            
            if (_navigationService.CurrentPageURI == NavigationHelper.Page2 &&  CanNavigateAway == false)
            {
                e.Cancel = true;
            }
        }


        #endregion 

    }
}
